<?php
namespace App\Http\Controllers\Business\Admin\Account;

use App\Models\Order;
use App\Models\Recharge;
use App\Models\Shop;
use App\Models\ShopAccount;
use App\Models\ShopSalesmanWithdrawal;
use App\Models\ShopSalesman;
use App\Models\ShopSalesmanDetail;
use App\Models\SalesmanInfo;
use App\Models\SalesmanSet;
use App\Models\SalesmanWithdraw;
use App\Models\SalesmanAccount;
use App\Models\SalesmanOrder;
use App\Models\SalesmanRegister;
use App\Models\SmsMessage;
use App\Models\RedEnvelope;
use App\Models\User;
use App\Models\Refund;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Log;

Class SalesmanManagement{
    //商家地推对账单统计数据
    public static function shopSalesmanStatementStatistic($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $shopCount = ShopSalesman::where('cityid', $cityId)->where('status', 1)->count();
        $commonRegisterNum = ShopSalesman::where('cityid', $cityId)->sum('commonregisternum');
        $partnerRegisterNum = ShopSalesman::where('cityid', $cityId)->sum('partnerregisternum');
        $registerNum = $commonRegisterNum + $partnerRegisterNum;
        $orderNum = ShopSalesman::where('cityid', $cityId)->sum('ordernum');
        $validOrderNum = ShopSalesman::where('cityid', $cityId)->sum('validordernum');
        $totalOrderAmount = ShopSalesman::where('cityid', $cityId)->sum('totalorderamount');
        $getMoney = ShopSalesman::where('cityid', $cityId)->sum('getmoney');
        $withdrawalMoney = ShopSalesman::where('cityid', $cityId)->sum('withdrawalmoney');
        $balance = ShopSalesman::where('cityid', $cityId)->sum('balance');
        $data = [
            'shopcount' => $shopCount,
            'registernum' => $registerNum,
            'ordernum' => $orderNum,
            'validordernum' => $validOrderNum,
            'totalorderamount' => $totalOrderAmount,
            'getmoney' => $getMoney,
            'withdrawalmoney' => $withdrawalMoney,
            'balance' => $balance,
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //商家地推对账单列表
    public static function shopSalesmanStatementList($cityId, $startTime, $lastTime, $page=1, $length=50) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime || !$lastTime) {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        $skip = ($page-1) * $length;
        $data = ShopSalesman::where('shop_salesman.cityid', $cityId)->where('shop_salesman.status', 1)
            ->where('shop_salesman.created_at', '>=', $startTime)->where('shop_salesman.created_at', '<=', $lastTime)
            ->join('shops', 'shop_salesman.shopid', '=', 'shops.id')->orderBy('shop_salesman.created_at', 'desc')
            ->select('shops.id as shopid', 'shops.name as shopname', 'shops.code as shopcode', 'shop_salesman.type as salesmantype', 'shop_salesman.commonregisternum',
                'shop_salesman.partnerregisternum','shop_salesman.ordernum', 'shop_salesman.validordernum', 'shop_salesman.totalorderamount', 'shop_salesman.getmoney',
                'shop_salesman.withdrawalmoney', 'shop_salesman.balance', 'shop_salesman.created_at')
            ->get();
        foreach($data as $k=>$v) {
            $data[$k]['registernum'] = $data[$k]['commonregisternum'] + $data[$k]['partnerregisternum'];
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //商家地推待提现表统计数据
    public static function shopSalesmanWaitWithdrawalStatistic($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $shopNum = ShopSalesmanWithdrawal::where('shop_salesman_withdrawal.status', 0)
            ->join('shop_salesman', 'shop_salesman_withdrawal.shopid', '=', 'shop_salesman.shopid')
            ->where('shop_salesman.cityid', $cityId)
            ->count();
        $waitWithdrawalMoney = ShopSalesmanWithdrawal::where('shop_salesman_withdrawal.status', 0)
            ->join('shop_salesman', 'shop_salesman_withdrawal.shopid', '=', 'shop_salesman.shopid')
            ->where('shop_salesman.cityid', $cityId)
            ->sum('shop_salesman_withdrawal.accountamount');
        $data = [
            'shopnum' => $shopNum,
            'waitwithdrawalmoney' => $waitWithdrawalMoney
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //准备商家待提现数据
    public static function readyShopSalesmanWaitWithdrawal($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        //TODO 商家打款数据这块由张总那边处理
        //普通待提现
        $commonSalesmanSet = SalesmanSet::where('cityid', $cityId)->where('salesmantype', 'shopsalesman')
            ->where('shoptype', 'common')->where('status', 1)->first();
        if($commonSalesmanSet) {
            $commonAccountPeriod = $commonSalesmanSet->accountperiod;
        } else {
            $commonAccountPeriod = 0;
        }
        //合伙人待提现
        $partnerSalesmanSet = SalesmanSet::where('cityid', $cityId)->where('salesmantype', 'shopsalesman')
            ->where('shoptype', 'partner')->where('status', 1)->first();
    }
    //商家地推待提现表列表
    public static function shopSalesmanWaitWithdrawalList($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = ShopSalesmanWithdrawal::where('shop_salesman_withdrawal.status', '0')
            ->join('shops', 'shop_salesman_withdrawal.shopid', '=', 'shops.id')
            ->where('shops.cityid', $cityId)
            ->select('shops.id as shopid','shops.code as shopcode','shops.name as shopname','shops.cardholder','shops.bankname','shops.bankcard','shops.bankprovince','shops.bankcity','shops.opencardbank', 'shop_salesman_withdrawal.accountamount', 'shop_salesman_withdrawal.id as withdrawalid')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //商家地推已提现统计数据
    public static function shopSalesmanWithdrawalStatistic($cityId) {

    }
    //商家地推已提现列表
    public static function shopSalesmanWithdrawalList($cityId, $startTime, $lastTime) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2017-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        $lists = ShopSalesmanWithdrawal::where('shop_salesman_withdrawal.accounttime','>=', $startTime)
            ->where('shop_salesman_withdrawal.accounttime', '<=', $lastTime)
            ->where('shop_salesman_withdrawal.status', '1')
            ->join('shops', 'shop_salesman_withdrawal.shopid', '=', 'shops.id')
            ->where('shops.cityid', $cityId)->orderBy('shop_salesman_withdrawal.created_at', 'desc')
            ->select('shops.id as shopid', 'shops.code as shopcode', 'shops.name as shopname', 'shops.cardholder', 'shops.bankcard',
                'shops.bankname', 'shops.bankprovince', 'shops.bankcity', 'shops.opencardbank', 'shop_salesman_withdrawal.id as withdrawalid',
                'shop_salesman_withdrawal.accounttime', 'shop_salesman_withdrawal.accountamount', 'shop_salesman_withdrawal.remark')
            ->get();
        $count = count($lists);
        $sum = ShopSalesmanWithdrawal::where('shop_salesman_withdrawal.accounttime','>=', $startTime)
            ->where('shop_salesman_withdrawal.accounttime', '<=', $lastTime)
            ->where('shop_salesman_withdrawal.status', '1')
            ->join('shops', 'shop_salesman_withdrawal.shopid', '=', 'shops.id')
            ->where('shops.cityid', $cityId)->orderBy('shop_salesman_withdrawal.created_at', 'desc')
            ->sum('shop_salesman_withdrawal.accountamount');
        $data = [
            'salesmantype' => 'shopsalesman',
            'count' => $count,
            'sum' => $sum,
            'cityid' => $cityId,
            'lists' => $lists,
            'starttime' => $startTime,
            'lasttime' => $lastTime
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //导出商家地推对账单
    public static function exportShopSalesmanStatement($cityId, $startTime, $lastTime) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime || !$lastTime) {
            return ['status'=>'failed', 'message'=>'请选择起始时间'];
        }
        $startTime = $startTime . " 00:00:00";
        $lastTime = $lastTime . " 23:59:59";
        $data = ShopSalesman::where('shop_salesman.cityid', $cityId)->where('shop_salesman.status', 1)
            ->where('shop_salesman.created_at', '>=', $startTime)->where('shop_salesman.created_at', '<=', $lastTime)
            ->join('shops', 'shop_salesman.shopid', '=', 'shops.id')->orderBy('shop_salesman.created_at', 'desc')
            ->select('shops.id as shopid', 'shops.name as shopname', 'shops.code as shopcode', 'shop_salesman.type as salesmantype', 'shop_salesman.commonregisternum',
                'shop_salesman.partnerregisternum','shop_salesman.ordernum', 'shop_salesman.validordernum', 'shop_salesman.totalorderamount', 'shop_salesman.getmoney',
                'shop_salesman.withdrawalmoney', 'shop_salesman.balance', 'shop_salesman.created_at')
            ->get();
        foreach($data as $k=>$v) {
            $data[$k]['registernum'] = $data[$k]['commonregisternum'] + $data[$k]['partnerregisternum'];
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //导出商家地推待提现表
    public static function exportShopSalesmanWaitWithdrawal($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = ShopSalesmanWithdrawal::where('shop_salesman_withdrawal.status', '0')
            ->join('shops', 'shop_salesman_withdrawal.shopid', '=', 'shops.id')
            ->where('shops.cityid', $cityId)
            ->select('shops.id as shopid','shops.code as shopcode','shops.name as shopname','shops.cardholder','shops.bankname','shops.bankcard','shops.bankprovince','shops.bankcity','shops.opencardbank', 'shop_salesman_withdrawal.accountamount', 'shop_salesman_withdrawal.id as withdrawalid')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //导出商家地推已提现表
    public static function exportShopSalesmanWithdrawal($cityId, $startTime, $lastTime) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            return ['status'=>'failed', 'message'=>'请选择起始时间'];
        }
        $startTime = $startTime . " 00:00:00";
        $lastTime = $lastTime . " 23:59:59";
        $data = ShopSalesmanWithdrawal::where('shop_salesman_withdrawal.created_at','>=', $startTime)
            ->where('shop_salesman_withdrawal.created_at', '<=', $lastTime)
            ->where('shop_salesman_withdrawal.status', '1')
            ->join('shops', 'shop_salesman_withdrawal.shopid', '=', 'shops.id')
            ->where('shops.cityid', $cityId)->orderBy('shop_salesman_withdrawal.created_at', 'desc')
            ->select('shops.id as shopid', 'shops.code as shopcode', 'shops.name as shopname', 'shops.cardholder', 'shops.bankcard', 'shops.bankname', 'shops.bankprovince', 'shops.bankcity', 'shops.opencardbank', 'shop_salesman_withdrawal.id as withdrawalid','shop_salesman_withdrawal.accounttime', 'shop_salesman_withdrawal.accountamount')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //给商家地推打款
    public static function remitSalesmanShop($withdrawalId, $remark='') {
        if(!$withdrawalId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $shopSalesmanWithdrawal = ShopSalesmanWithdrawal::where('id', $withdrawalId)->where('status', 0)->select('accountamount', 'shopid')->first();
        if(!$shopSalesmanWithdrawal) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $accountAmount = $shopSalesmanWithdrawal->accountamount;
        $shopId = $shopSalesmanWithdrawal->shopid;

        ShopSalesman::where('shopid', $shopId)->decrement('balance', $accountAmount);
        ShopSalesman::where('shopid', $shopId)->increment('withdrawalmoney', $accountAmount);
        $balance = ShopSalesman::where('shopid', $shopId)->pluck('balance')->first();
        $updateData = [
            'balance'=>$balance,
            'status'=>'1',
            'accounttime' => date('Y-m-d H:i:s'),
            'remark' => $remark,
            'adminid'=>Auth::guard('adminweb')->id()
        ];
        ShopSalesmanWithdrawal::where('id', $withdrawalId)->update($updateData);
        return ['status'=>'success', 'message'=>'打款成功'];
    }
    //用户地推对账单统计数据
    public static function userSalesmanStatementStatistic($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $userCount = SalesmanInfo::where('cityid', $cityId)->count();
        $registerNum = SalesmanInfo::where('cityid', $cityId)->sum('registernum');
        $orderNum = SalesmanInfo::where('cityid', $cityId)->sum('ordernum');
        $orderValidNum = SalesmanInfo::where('cityid', $cityId)->sum('ordervalidnum');
        $orderTotalAmount = SalesmanInfo::where('cityid', $cityId)->sum('ordertotalamount');
        $totalMoney = SalesmanInfo::where('cityid', $cityId)->sum('totalmoney');
        $withdrawalMoney = SalesmanInfo::where('cityid', $cityId)->sum('withdrawalmoney');
        $balance = SalesmanInfo::where('cityid', $cityId)->sum('balance');
        $data = [
            'usercount' => $userCount,
            'registernum' => $registerNum,
            'ordernum' => $orderNum,
            'ordervalidnum' => $orderValidNum,
            'ordertotalamount' => $orderTotalAmount,
            'getmoney' => $totalMoney,
            'withdrawalmoney' => $withdrawalMoney,
            'balance' => $balance
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //用户地推对账单列表
    public static function userSalesmanStatementList($cityId, $startTime, $lastTime, $page=1, $length=50) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime || !$lastTime) {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        $skip = ($page-1)*$length;
        $data = SalesmanInfo::where('salesman_info.cityid', $cityId)
            ->where('salesman_info.created_at', '>=', $startTime)->where('salesman_info.created_at', '<=', $lastTime)
            ->join('users', 'salesman_info.salesmanid', '=', 'users.id')
            ->orderBy('salesman_info.created_at', 'desc')
            ->select('users.mobile','salesman_info.name', 'salesman_info.salesmanid', 'salesman_info.registernum', 'salesman_info.ordernum', 'salesman_info.ordervalidnum',
                'salesman_info.ordertotalamount', 'salesman_info.totalmoney', 'salesman_info.withdrawalmoney', 'salesman_info.balance', 'salesman_info.status', 'salesman_info.created_at')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //用户地推待提现表统计数据
    public static function userSalesmanWaitWithdrawalStatistic($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $userCount = SalesmanWithdraw::where('salesman_withdraw.status', 0)
            ->join('salesman_info', 'salesman_withdraw.salesmanid', '=', 'salesman_info.salesmanid')
            ->where('salesman_info.cityid', $cityId)
            ->count();
        $waitWithdrawalMoney = SalesmanWithdraw::where('salesman_withdraw.status', 0)
            ->join('salesman_info', 'salesman_withdraw.salesmanid', '=', 'salesman_info.salesmanid')
            ->where('salesman_info.cityid', $cityId)
            ->sum('salesman_withdraw.accountmoney');
        $data = [
            'usercount' => $userCount,
            'waitwithdrawalmoney' => $waitWithdrawalMoney,
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //准备用户待提现列表
    public static function readyUserSalesmanWaitWithdrawal($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $salesmanSet = SalesmanSet::where('cityid', $cityId)->where('salesmantype', 'usersalesman')->where('status', 1)->first();
        if(!$salesmanSet) {
            return ['status'=>'failed', 'message'=>'获取信息错误'];
        }
        $accountPeriod = $salesmanSet->accountperiod;
        $lastWithdrawalDate = date('Y-m-01', strtotime('-'. $accountPeriod . 'months')) . " 00:00:00";
        $count = SalesmanWithdraw::where('salesman_withdraw.created_at', '>=', $lastWithdrawalDate)
            ->join('salesman_info', 'salesman_withdraw.salesmanid', '=', 'salesman_info.salesmanid')
            ->where('salesman_info.cityid', $cityId)->count();
        if($count > 0) {
            return ['status'=>'failed', 'message'=>'已经生成过待提现数据'];
        }
        $theEndDayOfMonth = date('Y-m-d', strtotime("$lastWithdrawalDate+1month-1day")). " 23:59:59";
        //注册
        $registerSalesmanIdArray = SalesmanInfo::where('salesman_info.cityid', $cityId)
            ->join('salesman_register', 'salesman_info.salesmanid', '=', 'salesman_register.salesmanid')
            ->where('salesman_register.created_at', '>=', $lastWithdrawalDate)->where('salesman_register.created_at', '<=', $theEndDayOfMonth)
            ->select('salesman_info.salesmanid')->distinct('salesman_info.salesmanid')->get();
        //订单
        $orderSalesmanIdArray = SalesmanInfo::where('salesman_info.cityid', $cityId)
            ->join('salesman_order', 'salesman_info.salesmanid', '=', 'salesman_order.salesmanid')
            ->where('salesman_order.created_at', '>=', $lastWithdrawalDate)->where('salesman_order.created_at', '<=', $theEndDayOfMonth)
            ->select('salesman_info.salesmanid')->distinct('salesman_info.salesmanid')->get();
        $salesmanIdPool = [];
        foreach($registerSalesmanIdArray as $registerSalesmanId) {
            if(!in_array($registerSalesmanId['salesmanid'], $salesmanIdPool)) {
                array_push($salesmanIdPool, $registerSalesmanId['salesmanid']);
            }
        }
        foreach($orderSalesmanIdArray as $orderSalesmanId) {
            if(!in_array($orderSalesmanId['salesmanid'], $salesmanIdPool)) {
                array_push($salesmanIdPool, $orderSalesmanId['salesmanid']);
            }
        }
        $accountMonth = date('Y-m-01');
        foreach($salesmanIdPool as $salesmanId) {
            $registerGetMoney = SalesmanRegister::where('salesmanid', $salesmanId)->where('isvalid', 1)->where('status', 0)
                ->where('created_at', '>=', $lastWithdrawalDate)->where('created_at', '<=', $theEndDayOfMonth)->sum('getmoney');
            $orderGetMoney = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)->where('status', 0)
                ->where('created_at', '>=', $lastWithdrawalDate)->where('created_at', '<=', $theEndDayOfMonth)->sum('getmoney');
            $getMoney = $registerGetMoney + $orderGetMoney;
            $getMoney = ($getMoney != '')? $getMoney : 0;
            $registerNum = SalesmanRegister::where('salesmanid', $salesmanId)->where('isvalid', 1)->where('status', 0)
                ->where('created_at', '>=', $lastWithdrawalDate)->where('created_at', '<=', $theEndDayOfMonth)->count();
            $orderNum = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)->where('status', 0)
                ->where('created_at', '>=', $lastWithdrawalDate)->where('created_at', '<=', $theEndDayOfMonth)->count();
            $orderTotalAmount = SalesmanOrder::where('salesmanid', $salesmanId)->where('isvalid', 1)->where('status', 0)
                ->where('created_at', '>=', $lastWithdrawalDate)->where('created_at', '<=', $theEndDayOfMonth)->sum('orderamount');
            $orderTotalAmount = ($orderTotalAmount != '')? $orderTotalAmount: 0;
            SalesmanWithdraw::create([
                'salesmanid' => $salesmanId,
                'accountmoney' => $getMoney,
                'accountmonth' => $accountMonth,
                'registernum' => $registerNum,
                'ordernum' => $orderNum,
                'ordertotalamount' => $orderTotalAmount,
                'status' => 0
            ]);
            //更新状态
            SalesmanRegister::where('salesmanid', $salesmanId)->where('status', 0)
                ->where('created_at', '>=', $lastWithdrawalDate)->where('created_at', '<=', $theEndDayOfMonth)->update(['status'=>1]);
            SalesmanOrder::where('salesmanid', $salesmanId)->where('status', 0)
                ->where('created_at', '>=', $lastWithdrawalDate)->where('created_at', '<=', $theEndDayOfMonth)->update(['status'=>1]);
        }
        return ['status'=>'failed', 'message'=>'更新成功'];
    }
    //用户地推待提现表列表
    public static function userSalesmanWaitWithdrawalList($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = SalesmanWithdraw::where('salesman_withdraw.status', 0)
            ->join('salesman_info', 'salesman_withdraw.salesmanid', '=', 'salesman_info.salesmanid')
            ->join('users', 'salesman_info.salesmanid', '=', 'users.id')
            ->where('salesman_info.cityid', $cityId)
            ->select('salesman_info.id', 'salesman_info.name', 'users.mobile', 'salesman_withdraw.accountmoney', 'salesman_withdraw.id as withdrawalid')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //用户地推已提现统计数据
    public static function userSalesmanWithdrawalStatistic() {

    }
    //用户地推已提现列表
    public static function userSalesmanWithdrawalList($cityId, $startTime, $lastTime) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2016-07-01 00:00:00';
            $lastTime = date('Y-m-d') . " 23:59:59";
        }
        $lists = SalesmanWithdraw::where('salesman_withdraw.accounttime','>=', $startTime)
            ->where('salesman_withdraw.accounttime', '<=', $lastTime)
            ->where('salesman_withdraw.status', '1')
            ->join('salesman_info', 'salesman_withdraw.salesmanid', '=', 'salesman_info.salesmanid')
            ->join('users', 'salesman_info.salesmanid', '=', 'users.id')
            ->where('salesman_info.cityid', $cityId)->orderBy('salesman_withdraw.created_at', 'desc')
            ->select('salesman_info.id', 'salesman_info.name', 'users.mobile', 'salesman_withdraw.accountmoney', 'salesman_withdraw.accounttime')
            ->get();
        $count = count($lists);
        $sum = SalesmanWithdraw::where('salesman_withdraw.accounttime','>=', $startTime)
            ->where('salesman_withdraw.accounttime', '<=', $lastTime)
            ->where('salesman_withdraw.status', '1')
            ->join('salesman_info', 'salesman_withdraw.salesmanid', '=', 'salesman_info.salesmanid')
            ->where('salesman_info.cityid', $cityId)
            ->sum('accountmoney');
        $data = [
            'salesmantype' => 'usersalesman',
            'count' => $count,
            'sum' => $sum,
            'cityid' => $cityId,
            'lists' => $lists,
            'starttime' => $startTime,
            'lasttime' => $lastTime
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //导出用户地推对账单
    public static function exportUserSalesmanStatement($cityId, $startTime, $lastTime) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime || !$lastTime) {
            return ['status'=>'failed', 'message'=>'请选择起始时间'];
        }
        $startTime = $startTime . " 00:00:00";
        $lastTime = $lastTime . " 23:59:59";
        $data = SalesmanInfo::where('salesman_info.cityid', $cityId)->where('salesman_info.status', 1)
            ->where('salesman_info.created_at', '>=', $startTime)->where('salesman_info.created_at', '<=', $lastTime)
            ->join('users', 'salesman_info.salesmanid', '=', 'users.id')
            ->orderBy('salesman_info.created_at', 'desc')
            ->select('users.mobile','salesman_info.name', 'salesman_info.salesmanid', 'salesman_info.registernum', 'salesman_info.ordernum', 'salesman_info.ordervalidnum',
                'salesman_info.ordertotalamount', 'salesman_info.totalmoney', 'salesman_info.withdrawalmoney', 'salesman_info.balance', 'salesman_info.status', 'salesman_info.created_at')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //导出用户地推待提现表
    public static function exportUserSalesmanWaitWithdrawal($cityId) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = SalesmanWithdraw::where('salesman_withdraw.status', 0)
            ->join('salesman_info', 'salesman_withdraw.salesmanid', '=', 'salesman_info.salesmanid')
            ->join('users', 'salesman_info.salesmanid', '=', 'users.id')
            ->where('salesman_info.cityid', $cityId)
            ->select('salesman_info.id', 'salesman_info.name', 'users.mobile', 'salesman_withdraw.accountmoney', 'salesman_withdraw.id as withdrawalid')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //导出用户地推已提现表
    public static function exportUserSalesmanWithdrawal($cityId, $startTime, $lastTime) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            return ['status'=>'failed', 'message'=>'请选择起始时间'];
        }
        $startTime = $startTime . " 00:00:00";
        $lastTime = $lastTime . " 23:59:59";
        $data = SalesmanWithdraw::where('salesman_withdraw.created_at','>=', $startTime)
            ->where('salesman_withdraw.created_at', '<=', $lastTime)
            ->where('salesman_withdraw.status', '1')
            ->join('salesman_info', 'salesman_withdraw.salesmanid', '=', 'salesman_info.salesmanid')
            ->join('users', 'salesman_info.salesmanid', '=', 'users.id')
            ->where('salesman_info.cityid', $cityId)->orderBy('salesman_withdraw.created_at', 'desc')
            ->select('salesman_info.id', 'salesman_info.name', 'users.mobile', 'salesman_withdraw.accountmoney', 'salesman_withdraw.accounttime')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //给用户个人地推打款
    public static function remitSalesmanUser($withdrawId) {
        if(!$withdrawId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $salesmanWithdraw = SalesmanWithdraw::where('id', $withdrawId)->where('status', 0)->select('accountmoney', 'salesmanid')->first();
        if(!$salesmanWithdraw) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $accountMoney = $salesmanWithdraw->accountmoney;
        $salesmanId = $salesmanWithdraw->salesmanid;
        SalesmanInfo::where('salesmanid', $salesmanId)->decrement('balance', $accountMoney);
        SalesmanInfo::where('salesmanid', $salesmanId)->increment('withdrawalmoney', $accountMoney);
        $balance = SalesmanInfo::where('salesmanid', $salesmanId)->pluck('balance')->first();
        $updateData = [
            'balance'=>$balance,
            'status'=>'1',
            'accounttime' => date('Y-m-d H:i:s'),
            'adminid'=>Auth::guard('adminweb')->id()
        ];
        SalesmanWithdraw::where('id', $withdrawId)->update($updateData);
        return ['status'=>'success', 'message'=>'打款成功'];
    }
}